package com.emmmya.pojo.excel;


import com.emmmya.harinCheck.annotation.Check;
import lombok.Data;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.awt.Color;

/**
 * 建议日后存入数据库
 * @author Harin
 * @version 1.0
 * @date 2022-05-14 20:25
 */
@Data
public class ExcelAutoPack {

    @Check(name="标题名称",desc = "默认与@Check的name属性相同的匹配，根据该值匹配value")
    private String titleName;

    @Check(name="次序",desc = "排序")
    private Integer sort;

    @Check(name="标题别名",desc = "拥有该值时才会替换")
    private String aliasTtileName;

    @Check(name="时间格式化器",desc ="当属性为时间格式时，依据该格式字符串来输出时间，默认yyyy-MM-dd HH:mm:ss")
    private String dataFormat;

    private HarinCellStyle cellStyle;

    public ExcelAutoPack() {
    }

    public ExcelAutoPack(String titleName, Integer sort) {
        this.titleName = titleName;
        this.sort = sort;
    }

    public ExcelAutoPack(String titleName, Integer sort, String aliasTtileName, String dataFormat) {
        this.titleName = titleName;
        this.sort = sort;
        this.aliasTtileName = aliasTtileName;
        this.dataFormat = dataFormat;
    }

    @Data
    public static class HarinCellStyle {
        @Check(name="字体颜色")
        private String color;

        @Check(name="字体名称")
        private String fontName;

        @Check(name="字体大小")
        private Short fontSize;

        @Check(name="背景颜色")
        private String backgroupColor;

        @Check(name="边框")
        private Integer border;


        @Check(name="水平位置")
        private boolean horizontalPosition;

        @Check(name="垂直位置")
        private boolean verticalPosition;

        public CellStyle conversion(Workbook wb){
            XSSFWorkbook xwb = null;
            HSSFWorkbook hwb = null;
            CellStyle cellStyle = null;
            //缺少非空判断
            if(wb instanceof XSSFWorkbook){
                xwb = (XSSFWorkbook)wb;
                cellStyle = xwb.createCellStyle();
                XSSFCellStyle xssfcellStyle = (XSSFCellStyle) cellStyle;
                int[] fontColorRgb = hex2RGB(color);
                int[] bgColorRgb = hex2RGB(backgroupColor);
                //转成颜色对象
                XSSFColor fontColor = new XSSFColor(new Color(fontColorRgb[0], fontColorRgb[1], fontColorRgb[2]), null);
                XSSFColor gbColor = new XSSFColor(new Color(bgColorRgb[0], bgColorRgb[1], bgColorRgb[2]), null);

                //创建字体
                XSSFFont font = xwb.createFont();
                //设置字体颜色
                font.setColor(fontColor);
                font.setFontHeightInPoints(fontSize);

                //设置字体
                xssfcellStyle.setFont(font);

                //设置背景颜色
                xssfcellStyle.setFillForegroundColor(gbColor);
                xssfcellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

            }else{
                hwb = (HSSFWorkbook)wb;
                cellStyle = hwb.createCellStyle();

                int[] fontColorRgb = hex2RGB(color);
                int[] bgColorRgb = hex2RGB(backgroupColor);
                //转成颜色对象
                HSSFPalette pallette = hwb.getCustomPalette();//获取调色板

                //创建字体
                HSSFFont font = hwb.createFont();

                //设置字体颜色
                font.setColor(IndexedColors.SEA_GREEN.index);
                pallette.setColorAtIndex(IndexedColors.SEA_GREEN.index,(byte)fontColorRgb[0], (byte)fontColorRgb[1], (byte)fontColorRgb[2]);
                font.setFontHeightInPoints(fontSize);

                //设置字体
                cellStyle.setFont(font);
                HSSFPalette customPalette = hwb.getCustomPalette();//获取调色板
                //设置背景颜色
                cellStyle.setFillForegroundColor(IndexedColors.LIME.index);
                customPalette.setColorAtIndex(IndexedColors.LIME.index,(byte)bgColorRgb[0], (byte)bgColorRgb[1], (byte)bgColorRgb[2]);
                cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

            }
            parseBorder(border,cellStyle);
            if(horizontalPosition){
                cellStyle.setAlignment(HorizontalAlignment.CENTER);
            }
            if(verticalPosition){
                cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            }

            return cellStyle;
        }

        private void parseBorder(Integer border,CellStyle cellStyle){
            String s = Integer.toBinaryString(border);
            String format = String.format("%04d", Integer.valueOf(s));
            char[] chars = format.toCharArray();
            if(chars[0] == '1'){
                cellStyle.setBorderTop(BorderStyle.THIN);
                cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
            }
            if(chars[1] == '1'){
                cellStyle.setBorderRight(BorderStyle.THIN);
                cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
            }
            if(chars[2] == '1'){
                cellStyle.setBorderBottom(BorderStyle.THIN);
                cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
            }
            if(chars[3] == '1'){
                cellStyle.setBorderLeft(BorderStyle.THIN);
                cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
            }


        }
    }

    /**
     * 16进制颜色字符串转换成rgb
     * @param hexStr
     * @return rgb
     */
    public static int[] hex2RGB(String hexStr){
        if(hexStr != null && !"".equals(hexStr) && hexStr.length() == 7){
            int[] rgb = new int[3];
            rgb[0] = Integer.valueOf(hexStr.substring( 1, 3 ), 16);
            rgb[1] = Integer.valueOf(hexStr.substring( 3, 5 ), 16);
            rgb[2] =Integer.valueOf(hexStr.substring( 5, 7 ), 16);
            return rgb;
        }
        return null;
    }
}
